using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FastShip88;
using System.Linq;

public partial class Default_master : System.Web.UI.MasterPage {

    public string IsLogined {
        get {
            if (null == this.Session["IsLogined"])
                return "none";
            else
                return this.Session["IsLogined"].ToString(); 
        }
        set {
            this.Session["IsLogined"] = value;
        }
    }


    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        if (null == Membership.GetUser())
        {
            IsLogined = "none";
            return;
        }

        int userId =0;

        if (this.Session["IsLogined"] != null)
            return;

        if (this.Session["UserId "] == null)
        {

            System.Guid guidUser = (System.Guid)Membership.GetUser().ProviderUserKey;

            FastShip88.DBAccess.UsersDataContext db = new FastShip88.DBAccess.UsersDataContext();
            var users = db.UserInfos;
            var q_users = (from u in db.UserInfos
                           where u.UserId == guidUser
                           select u);
            if (q_users.Count() > 0)
            {
                var q = q_users.First();
                userId = q.Id;
                this.Session["UserId"] = userId.ToString();
            }
            else
            {
                FastShip88.Utility.Log(FastShip88.Utility.LogLevel.Error, "No user id for user key " + guidUser.ToString());
            }
        }
        else
        {
            int.TryParse(this.Session["UserId"].ToString(), out userId);

        }

        if (userId > 0)
        {
            FastShip88.DBAccess.UsersDataContext db = new FastShip88.DBAccess.UsersDataContext();
 
            var q_bills = from u in db.UserBills
                          where u.UserId == userId && u.FeeTypeId == 13 && u.IsCancelled == false && u.IsCleared == false
                          select new { u.Id };

            if( q_bills.Count() > 0)
                IsLogined = "";

        }
    }

}